package school.valon.business.impl;

import org.springframework.transaction.annotation.Transactional;

import school.valon.business.PaymentBusiness;
import school.valon.business.base.BaseBusiness;
import school.valon.dto.ExpensePaymentDto;
import school.valon.dto.PaymentDto;
import school.valon.persistence.dao.ExpenseCategoryDao;
import school.valon.persistence.dao.ExpensePaymentDao;
import school.valon.persistence.dao.PaymentDao;
import school.valon.persistence.dao.UserDao;
import school.valon.persistence.pojo.ExpenseCategory;
import school.valon.persistence.pojo.ExpensePayment;
import school.valon.persistence.pojo.ExpensePaymentPK;
import school.valon.persistence.pojo.Payment;

@Transactional
public class PaymentBusinessImpl extends BaseBusiness implements PaymentBusiness {

	private PaymentDao paymentDao;
	private ExpenseCategoryDao expenseCategoryDao;
	private ExpensePaymentDao expensePaymentDao;
	private UserDao userDao;

	public void setPaymentDao(PaymentDao paymentDao) {
		this.paymentDao = paymentDao;
	}

	public void setExpenseCategoryDao(ExpenseCategoryDao expenseCategoryDao) {
		this.expenseCategoryDao = expenseCategoryDao;
	}

	public void setExpensePaymentDao(ExpensePaymentDao expensePaymentDao) {
		this.expensePaymentDao = expensePaymentDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public PaymentDto makePayment(PaymentDto paymentDto) {
		Payment payment = mapper.map(paymentDto, Payment.class);
		if (paymentDto.getPaymentType() != null && paymentDto.getPaymentType().getPaymentTypeId() != null) {
			payment.setPaymentType(
					paymentDao.findPaymentType(paymentDto.getPaymentType().getPaymentTypeId()));
		}
		if (paymentDto.getUser() != null && paymentDto.getUser().getUserId() != null) {
			userDao.find(paymentDto.getUser().getUserId());
		}
		PaymentDto newPaymentDto = mapper.map(paymentDao.create(payment), PaymentDto.class);
		if (newPaymentDto.getUser() != null) newPaymentDto.getUser().setPayments(null);
		newPaymentDto.setExpensePayments(null);
		if (newPaymentDto.getPaymentType() != null) newPaymentDto.getPaymentType().setPayments(null);
		return newPaymentDto;
	}

	public ExpensePaymentDto makeExpensePayment(ExpensePaymentDto expensePaymentDto) {
		ExpensePayment expensePayment = mapper.map(expensePaymentDto, ExpensePayment.class);
		Payment payment = null;
		ExpenseCategory expenseCategory = null;
		expensePayment.setId(new ExpensePaymentPK());
		if (expensePaymentDto.getPayment() == null || expensePaymentDto.getPayment().getPaymentId() == null) {
			payment = paymentDao.create(mapper.map(expensePaymentDto.getPayment(), Payment.class));
		} else {
			payment = paymentDao.find(expensePaymentDto.getPayment().getPaymentId());
		}

		if (expensePaymentDto.getExpenseCategory() != null
				&& expensePaymentDto.getExpenseCategory().getExpenseCategoryId() != null) {
			expenseCategory = expenseCategoryDao.find(expensePaymentDto.getExpenseCategory().getExpenseCategoryId());			
		}

		expensePayment.getId().setExpenseCategoryId(
				expensePaymentDto.getExpenseCategory().getExpenseCategoryId());
		expensePayment.getId().setPaymentId(payment.getPaymentId());
		expensePayment.setExpenseCategory(expenseCategory);
		expensePayment.setPayment(payment);
		
		ExpensePaymentDto newExpensePaymentDto
			= mapper.map(expensePaymentDao.create(expensePayment), ExpensePaymentDto.class);
		if (newExpensePaymentDto.getPayment().getPaymentType() != null) newExpensePaymentDto.getPayment().getPaymentType().setPayments(null);
		newExpensePaymentDto.getPayment().setUser(null);
		newExpensePaymentDto.getPayment().setExpensePayments(null);
		newExpensePaymentDto.getExpenseCategory().setExpensePayments(null);
		return newExpensePaymentDto;
	}
}
